package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.entity.SchoolTeacher;

public class SchoolTeacherRowMapper implements RowMapper<SchoolTeacher> {

    private int type;
    
    public static final int AND_CLASSTNAME = 1;
    
    public static final int AND_SCHOOLNAME = 2;
    
    public static final int AND_TEACHERINFO = 3;
    
    public static final int AND_TEACHERACCOUNT = 4;
    
    public static final int AND_CARD = 5;
    public static final int  AND_NEW_STEACHER=6;

    public SchoolTeacherRowMapper() {
    }

    public SchoolTeacherRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SchoolTeacher mapRow(ResultSet rs, int rowNum) throws SQLException {
        SchoolTeacher st = parseSchoolTeacher(new SchoolTeacher(), rs);

        switch (type) {
            case AND_CLASSTNAME:
                st.setAccount(rs.getString("account"));
                st.setGname(rs.getString("gname"));
                st.setSex(rs.getString("sex"));
                st.setClassNams_in(rs.getString("classNams_in"));
                st.setClassName_manage(rs.getString("className_manage"));
                break;
            case AND_SCHOOLNAME:
                st.setName_school(rs.getString("name_school"));
                break;
            case AND_TEACHERINFO:
                st.setGname(rs.getString("gname"));
                st.setClassNams_in(rs.getString("classNams_in"));
                st.setDesinfo(rs.getString("desinfo"));
                st.setHeadimg(rs.getString("headimg"));
                break;
            case AND_TEACHERACCOUNT:
            	st.setAccount(rs.getString("account"));
            	break;
            case AND_CARD:
                st.setCardnum(rs.getString("cardnum"));
                st.setClassNams_in(rs.getString("classNams_in"));
                st.setCtime_str(SystemDateFormatUtil.formatDate(st.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
                break;
            case AND_NEW_STEACHER:
            	st.setAccount(rs.getString("phone"));
            	st.setGname(rs.getString("gname"));
            	break;
        }
        return st;
    }

    public static SchoolTeacher parseSchoolTeacher(SchoolTeacher s, ResultSet rs) throws SQLException {
        return parseSchoolTeacher(s, rs, null);
    }

    public static SchoolTeacher parseSchoolTeacher(SchoolTeacher s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            s.setUid(null);
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setTname(rs.getString(su+"tname"));
        } catch (SQLException e) {
            s.setTname(null);
        }
        try {
            s.setGid(rs.getLong(su+"gid"));
        } catch (SQLException e) {
            s.setGid(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        try {
            s.setIsadmin(rs.getShort(su+"isadmin"));
        } catch (SQLException e) {
            s.setIsadmin(null);
        }
        try {
            s.setMesCount(rs.getInt(su+"mesCount"));
        } catch (SQLException e) {
            s.setMesCount(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SchoolTeacher s) {
        par.put("tid",s.getTid());
        par.put("uid",s.getUid());
        par.put("sid",s.getSid());
        par.put("tname", s.getTname());
        par.put("gid",s.getGid());
        par.put("ctime",s.getCtime());
        par.put("isadmin",s.getIsadmin());
        par.put("mesCount",s.getMesCount());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SchoolTeacher s) {
        args.put("tid",s.getTid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SchoolTeacher s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "tid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(SchoolTeacher s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into school_teacher(");
        int count = 0;
        if (null != s.getUid()) {
            sql.append("uid,");
            count++;
        }

        if (null !=s.getSid()) {
            sql.append("sid,");
            count++;
        }
        if (null !=s.getTname()) {
            sql.append("tname,");
            count++;
        }
        if (null != s.getGid()) {
            sql.append("gid,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        if (null != s.getIsadmin()) {
            sql.append("isadmin,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SchoolTeacher s)
        throws SQLException {
        int count = 1;
        if (null != s.getUid()) {
            ps.setLong(count++,s.getUid());
        }
        if (null != s.getSid()) {
            ps.setInt(count++, s.getSid());
        }
        if (null != s.getTname()) {
            ps.setString(count++, s.getTname());
        }
        if (null != s.getGid()) {
            ps.setLong(count++,s.getGid());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }
        if (null != s.getIsadmin()) {
            ps.setShort(count++,s.getIsadmin());
        }
       


    }

}
